var dksCtrl  = new XFApp.client();
XFApp.client.prototype.dockerstatus = {
    get : function(status){
        switch (status){
            case "32" : return "申报失败";
            case "33" : return "终止申报";
            case "81" : return "清单申报中";
            case "100" : return "离境申报完成";
            default : return "未定义状态";
        }
    }
};
/**
 *@Description 订单首页
 *@Author zhangnf
 *@Date 2015-09-23
 */
var orderCtrl  = new XFApp.client();
XFApp.client.prototype.order = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryByPageURL : basePath+"/nt/transData/getOrderPage",
        uploadURL: basePath+"/nt/transData/fileUpload"
    },
    queryPage : function(){
        _this = this;
        if(!!_this.page){
            _this.page.page("remote",0,$('#searchForm').serialize());
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                remote: {
                    url: _this.urlItems.queryByPageURL,
                    params: $('#searchForm').serialize(),
                    success: function (data, pageIndex) {
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(null == data
                                || null == data.result
                                || null == data.result.itemList
                                || data.result.itemList.length < 1){
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }else{
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    var pageNo = data.result.pageNo-1;
                                    element.id=parseInt(index)+1+parseInt(pageNo)*10;
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }
                        }else{
                            window.webDialogUtil.alert("提示","操作失败");
                        }
                    },
                    error : function(){
                        window.webDialogUtil.alert("提示","操作失败");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    ajaxDownload : function(){
            $.ajax({
                url:basePath+"/nt/transData/exportOrderData",
                data:$('#searchForm').serialize(),
                type:'get',
                cache:false,
                success:function(data) {
                    if(data.success){
                        var url = basePath+"/nt/transData/download?fileName="+data.fileName;
                        $("body").append("<iframe id='file_download_frame' src='" + url +"' frameborder=0 width=0 height=0 style='display:none'></iframe>");
                    }else{
                        window.webDialogUtil.alert("提示",data.errMsg);
                    }
                },
                error : function() {
                    window.webDialogUtil.alert("提示","操作失败");
                }
            });
    },

    dataFormat : function(timeLong){
        if(null == timeLong
            || "" == timeLong){
            return "";
        }
        return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
    },
    event : function(){
        _this = this;

        $("#searchButton").on("click",function(){
            orderCtrl.order.queryPage();
        });

        $("#exportButton").on("click",function(){
            orderCtrl.order.ajaxDownload();
        });

        orderCtrl.order.queryPage();
    }
};
$.views.converters({
    ftime : function(value) {
        return orderCtrl.order.dataFormat(value) ;
    },
    fdkstatus : function(value){
        return dksCtrl.dockerstatus.get(value);
    },
    fmoney : function(value){
        if(null == value
            || "" == value){
            return "";
        }
        return orderCtrl.fmoney(value);
    },
    fmoneyUnit : function(value){
        if(!value){
            return "";
        }
        for(index in stats_currencys){
            var currencys = stats_currencys[index];
            if(currencys.dictCode == value){
                return currencys.dictName;
            }
        }
        return  "未知类型("+value+")";
    }
});
$(function(){
    /**
     * 跟踪状态为指定定条件，重写渲染方法
     * @param dictTypeCode
     * @param valueInput
     * @param displayId
     */
    XFApp.client.prototype.dictionary.load = function(dictTypeCode,valueInput,displayId){
        _this = this;
        if(null == displayId){
            displayId = dictTypeCode;
        }
        var dictTypeId = this.getTypeId(dictTypeCode);

        $("#"+displayId).attr("data-id","");
        //$("#"+valueInput).val("");
        $("#"+displayId).bind("blur",function(){
            $("#"+valueInput).val($("#"+displayId).attr("data-id"));
        });
        $.ajax( {
            url:basePath+"/nt/dict/getByDictTypeId?dictTypeId="+dictTypeId,
            data:{
                dictTypeId : dictTypeId
            },
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                var result = data.result;
                var array = new Array();
                for(var i = 0; i < result.length; i++){
                    var item = result[i];
                    if(item.dictCode == '32' || item.dictCode == '33' || item.dictCode == '81' || item.dictCode == '100'){
                        array.push(item);
                    }
                }
                data.result = array;
                $("#"+displayId).bsSuggest({
                    idField: "dictCode",
                    keyField: "dictName",
                    effectiveFields :["dictCode","dictName"],
                    effectiveFieldsAlias:{dictCode:"编码",dictName:"名称"},
                    data: {
                        'value':data.result
                    }
                });
                var val = $("#"+valueInput).val();
                if(""!=val){
                    $.each(data.result, function(i,item){
                        if(item.dictCode == val){
                            $("#"+displayId).val(item.dictName);
                            $("#"+displayId).attr("data-id",val);
                            return true;
                        }
                    });
                }

            },
            error : function() {
                console.log("字典查询异常");
            }
        });
    };
    dictCtrl.dictionary.load("orderDocketStatus","docketStatusHidden");
    orderCtrl.order.event();
    var $tb = $("#docketStatusHidden").next();
});